<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Notice;

//ini_set('date.timezone','Asia/Shanghai');
class NoticeInterval extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'NoticeInterval';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'tick send notice to game';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        \Log::info('crontab:NoticeInterval'.' START', []);

        $valid_time = time();
        $list = \DB::connection('mysql')->table('notice_interval')->select('id', 'area', 'area_list','gap', 'trigger_time', 'trigger_time', 'send_time', 'send_times','title', 'content','game_msgid')
            ->where('start_time', '<=', $valid_time)->where('end_time', ">=", $valid_time)
            //->whereRaw('limit_times', ">", 'send_times')
            ->get();

        // $url = env('APP_URL'). "/admin/mail/api-send";

        \Log::info('crontab:NoticeInterval'. ' count = '. $list->count() );
        $log_ids = '';
        // /admin/mail/api-send?id=143&status=1
        foreach ($list as $obj) {

                // $this->curl_request($url, ['id' => $obj->id, 'status' => 1]);
            $obj->game_msgid ?: \App\Service\MessageDef::p2g_notice_request;
            if (! $this->checkSend($obj) ) {
                continue;
            }
            Notice::send($obj, $obj->area);

            $obj->send_times ++;
            $update_data = ['send_time' => time(), 'send_times' => $obj->send_times];
            \DB::connection('mysql')->table('notice_interval')->where('id', $obj->id)->update($update_data);
            $log_ids = true;
        }
        \Log::info('crontab:NoticeInterval'. ' END', [$log_ids]);
        exit;
        
    }

    // 条件判断
    public function checkSend($mail_interval, $compare_time = null){
        $compare_time =  $compare_time?: time();

        $next_t = $mail_interval->send_time + $obj->gap;  

        // 如果小于一天 则不用 trigger_time
        if ($mail_interval->gap < 86400 ) {
            
            if ( $compare_time > $next_t) {
                return false;
            }
            return true;
        }

        if (! stripos($mail_interval->trigger_time, ':') || strlen($mail_interval->trigger_time) < 5 ) {
            \Log::error('[NOTICE_INTERVAL][TRIGGER_TIME] is not valid');
            return false;
        }

        $str = date("Y-m-d", $next_t);
        $str .= ' '.$mail_interval->trigger_time;
        $new_time = strtotime($str);
        if ($new_time <= $compare_time && $mail_interval->send_time <= $new_time) {
            return true;
        }

        return false;
    }

    

}
